package com.scap.authen.service;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.scap.authen.dao.ApplicationDao;
import com.scap.authen.dao.ApplicationDaoImpl;
import com.scap.authen.dao.SiteDao;
import com.scap.authen.entity.Application;
import com.scap.authen.util.JDate;
import com.scap.authen.util.UploadFile;
import com.scap.authen.util.ValidateField;

@Service("applicationService")
@Transactional
public class ApplicationServiceImpl implements ApplicationService{
	
	@Autowired
	private ApplicationDao voApplicationDao;
	@Autowired
	private ApplicationContext context;

	public List<Object> getAllApplication(){

		return voApplicationDao.getAllApplication();
		
	}
	
	
	public String insertApplication(Application voApplication,MultipartFile voFile,BindingResult result,HttpServletRequest request){
		
		String vnResult = "";
		
		
		String vnPath = request.getRealPath("/resources/images/");
		ValidateField voValidateField = new ValidateField();
		voValidateField.getObjectError(result);
		
		if(!result.hasErrors()){
			
			UploadFile voUploadFile = new UploadFile();
			 JDate voJdate = new JDate();
			 
			 if(voUploadFile.uploadFileHandler(voFile,vnPath)){
				 voApplication.setVaImageUrl("application/"+voFile.getOriginalFilename());
			 }else{
				 voApplication.setVaImageUrl("noimg.png");
			 }
				 voApplication.setVaMenuStatus("N");
				 voApplication.setVaInsertDate(voJdate.getDate());
				 voApplication.setVaInsertTime(voJdate.getTime());
				 voApplication.setVnInsertUserId(1);
			
			if (voApplicationDao.insertApplication(voApplication)) {
				vnResult = "success";
			} else {
				vnResult = "fail";
			}
		}

		return vnResult;
	}
	
	public String updateApplication(Application voApplication,MultipartFile voFile,BindingResult result,HttpServletRequest request ,String vnUploadType){
		
		String vnResult = "";
		
		String vnPath = request.getRealPath("/resources/images/");
		ValidateField voValidateField = new ValidateField();
		
		if(vnUploadType.equals("yes") &&
				voFile.getOriginalFilename().equals("") || voFile.getOriginalFilename()== null){
//			result.addError(new ObjectError("vaImageUrl",""));
			result.rejectValue("vaImageUrl", "NotEmpty.application.vaImageUrl");
		}

		voValidateField.getObjectError(result);
		
		if(!result.hasErrors()){
			
			UploadFile voUploadFile = new UploadFile();
			 JDate voJdate = new JDate();
	 
			if(!voFile.getOriginalFilename().equals("") || voFile.getOriginalFilename()!= null){
				if(voUploadFile.uploadFileHandler(voFile,vnPath)){
					 voApplication.setVaImageUrl("application/"+voFile.getOriginalFilename());
				 }else{
					 voApplication.setVaImageUrl("noimg.png");
				 }
			}
			 
				 voApplication.setVaMenuStatus("N");
				 voApplication.setVaUpdateDate(voJdate.getDate());
				 voApplication.setVaUpdateTime(voJdate.getTime());
				 voApplication.setVnUpdateUserId(1);
			
			if (voApplicationDao.updateApplication(voApplication)) {
				vnResult = "success";
			} else {
				vnResult = "fail";
			}
		}

		return vnResult;
	}

	@Override
	public String deleteApplication(Application voApplication,HttpServletRequest request) {
		
		String vnResult = "";
		Locale voLocale = RequestContextUtils.getLocale(request);
		
		if (voApplicationDao.deleteApplication(voApplication)) {
			vnResult = "success";
		} else {
			vnResult = "fail";
		}
		
		return vnResult;
	}
	
	public List<Object> getListBasedOnSearchParameter(String searchParameter,List<Object> applicationsList) {
		
		if (null != searchParameter && !searchParameter.equals("")) {
			List<Object> applicationsListForSearch = new ArrayList<Object>();
			searchParameter = searchParameter.toUpperCase();
			for (int i=0; i<applicationsList.size(); i++) {
				
				Application application = (Application) applicationsList.get(i);
				if (application.getVaApplicationId().toUpperCase().indexOf(searchParameter)!= -1 || application.getVaApplicationName().toUpperCase().indexOf(searchParameter)!= -1
						|| application.getVaImageUrl().toUpperCase().indexOf(searchParameter)!= -1 || application.getVaLink().toUpperCase().indexOf(searchParameter)!= -1
						|| application.getVaMenuStatus().toUpperCase().indexOf(searchParameter)!= -1) {
					applicationsListForSearch.add(application);					
				}
				
			}
			applicationsList = applicationsListForSearch;
			applicationsListForSearch = null;
		}
		return applicationsList;
	}
}
